// 添加评论
import {Comment} from "~/server/models/user";

export default defineEventHandler(async (event) => {
    try {
        const method = event.node.req.method;

        if (method !== 'POST') {
            const errorMessage = `不支持的方法: ${method}`;
            throw new Error(errorMessage);
        }

        const body = await readBody(event);

        // 确保body包含必要的字段，这里仅为示例，实际情况可能需要更多验证
        if (!body) {
            throw new Error("缺少必要的信息");
        }

        const newComment = await Comment.create(body);
        if (!newComment) {
            throw new Error("创建失败");
        }

        return {message: "创建成功"};
    } catch (error) {
        console.error("操作过程中发生错误:", error);
        return {
            error: "操作过程中发生错误",
            details: error instanceof Error ? error.message : "未知错误",
        };
    }
});
